(function($, doc) {
	$.init();
	var userinfo = app.getState() || {};
    var Info;
    
	$.plusReady(function() {
		var width=document.documentElement.clientWidth;
    	var height=document.documentElement.clientHeight;
    	document.getElementById('account_pwd').style.width=width+'px';
    	document.getElementById('account_pwd').style.height=height+'px';
		
		var other_uid = plus.webview.currentWebview().other_uid || '';
		var other_phone = plus.webview.currentWebview().other_phone || '';
		var other_headimgurl = plus.webview.currentWebview().other_headimgurl || '';
		var other_nickname = plus.webview.currentWebview().other_nickname || '';
		var type = plus.webview.currentWebview().type || 0;
		
		if(type == 1) {
			doc.getElementById('inp_phone').style.display = 'none';
			doc.getElementById('account').value=other_uid;
			doc.getElementById('account').disabled='disabled';
		}
		if(type == 0) {
			doc.getElementById('inp_phone').style.display = 'block';
		}


        /*
		doc.getElementById('convert').addEventListener('tap', function() {
			  //打开兑换节点页
			  mui.openWindow({
			    id:'convert.html',
			    url:'convert.html',
			    show: {
					aniShow: 'pop-in'
				},
				waiting: {
					autoShow: false
				}
			  });
		}, false);*/
		
		 
		doc.getElementById('transfer_out_record').addEventListener('tap', function() {
			//打开转出记录页
			mui.openWindow({
			    id:'roll_out_record.html',
			    url:'roll_out_record.html',
			    show: {
					autoShow:false
				},
				 waiting:{
			       autoShow:true,//自动显示等待框，默认为true
			       title:'正在加载...'//等待对话框上显示的提示内容
			   }
			});
		}) 		
		
		
		//  点击确认转出
		doc.getElementById('do_shift').addEventListener('tap', function() {
            var account= doc.getElementById('account').value;
            var money = doc.getElementById('money').value;
			money = parseFloat(money);
			
			Info={
				user_id: userinfo.user_id,
				account: account,
				money: money,
				type: type,
				_token:userinfo.token
			};
			
            if(account.trim()==''){
				plus.nativeUI.toast('对方的ID不能为空');
				return;
			}
            
			if(account.trim()==userinfo.user_id || account.trim()==userinfo.mobile){
				plus.nativeUI.toast('收款人不能是自己');
				return;
			}
			/*
			if(!userinfo.banks_card){
				plus.nativeUI.toast('请先绑定你的银行卡');
				return;
			}
	        */
			if(money == '' || money <= 0) {
				plus.nativeUI.toast('转出余额不能小于1');
				return;
			}
			if(money > userinfo.a1_money) {
				plus.nativeUI.toast('转出余额大于会员当余额');
				return;
			}
			
			if(type == 0) {
				if(doc.getElementById('last_phone').value.trim() == '') {
					plus.nativeUI.toast('请输入对方手机号末4位数');
					return;
				}
				Info.last_phone=doc.getElementById('last_phone').value.trim();
			}
			
			document.getElementById('account').blur();
			document.getElementById('money').blur();
			document.getElementById('last_phone').blur();
			mui.ajax({
				url: apiurl + "Shift/check_ouser/",
				data: Info,
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'post',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					if(res.status ==-3){
						mui.toast(res.msg);
						app.out();
						return false;
					}
					// 请求成功
					if(res.status < 1) {
						mui.alert(res.msg);
					} else {
						Info.to_id=res.data.user_id;
						//显示密码框
						document.getElementById('account_pwd').style.display="block";
						document.getElementById('main_content').style.display="block";
						document.getElementById('keyboard').style.display="block";
						var pwd_array=mui('.mui-input-row.mui-password input[type="password"]');
						mui.each(pwd_array,function(index,item){
							if(item.value!=''){
							   	item.value='';
							}
						}) ;
					}
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('获取数据失败！');
				}
			});

		});
		
		
		document.getElementById('close_win').addEventListener('tap', function() {
			document.getElementById('account_pwd').style.display="none";
			document.getElementById('main_content').style.display="none";
			document.getElementById('keyboard').style.display="none";
		})
		
		//点击删除
		mui('#keyboard').on('tap', '#numC', function() {
			var pwd_array=mui('.mui-input-row.mui-password input[type="password"]');
			var len=pwd_array.length;
			for(i=len-1;i>=0;i--){
				if(pwd_array[i].value!=''){
				 	pwd_array[i].value='';
				 	break;
				}
			}
		});
			
			
		//点击数字
			mui('#keyboard').on('tap', '.num.data_num', function() {
				var number=this.getAttribute('data-num');
				var pwd_array=mui('.mui-input-row.mui-password input[type="password"]');
				var f=false;
				var pwd='';
				mui.each(pwd_array,function(index,item){
				   if(item.value==''){
				   	   item.value=number;
				   	   return false;
				   }
				}) ;

				mui.each(pwd_array,function(index,item){
				   if(item.value==''){
				   	   f=true;
				   	   return false;
				   }
				   pwd+=item.value;
				}) ;
				
				if(!f){
					//mui.alert('正在后台处理...');
					Info.trade_pwd=pwd;
                    document.getElementById('account_pwd').style.display="none";
				    document.getElementById('main_content').style.display="none";
					document.getElementById('keyboard').style.display="none";
					plus.nativeUI.showWaiting(); 
					mui.ajax({
						url: apiurl + "Shift/do_shift_user/",
						data: Info,
						async: true,
						dataType: 'json',
						crossDomain: true, //强制使用5+跨域
						type: 'post',
						timeout: 10000,
						success: function(res) {
							plus.nativeUI.closeWaiting();  
							console.log(JSON.stringify(res));
							
							if(res.status ==-3){
								mui.toast(res.msg);
								app.out();
								return false;
							}
							// 请求成功
							if(res.status < 1) {
								mui.alert(res.msg);
							} else {
								plus.nativeUI.toast(res.msg);
								userinfo.a1_money = res.data.a1_money;
								userinfo.a2_money = res.data.a2_money;
								app.setState(userinfo);
								
						        mui.fire(plus.webview.getLaunchWebview(), 'refresh_amoney');
						        mui.back();
							}
						},
						error: function(xhr, type, errorThrown) {
							//console.log(JSON.stringify(xhr));
							//console.log(JSON.stringify(type));
							//console.log(JSON.stringify(errorThrown));
							mui.toast('操作失败！');
						}
					});
				}
			   
			})
			
		
	})
	
}(mui, document));


	function set_money(obj) {
		var sNum = obj.value;
		var nNum = toDecimal2(sNum);
		if(!isNaN(nNum)) {
			document.getElementById('pay').innerHTML = nNum;
		}
		
	}
	
	//制保留2位小数，如：2，会在2后面补上00.即2.00 
	function toDecimal2(x) {
		var f = parseFloat(x);
		if(isNaN(f)) {
			return '';
		}
		var f = Math.floor(x * 100) / 100;
		var s = f.toString();
		var rs = s.indexOf('.');
		if(rs < 0) {
			rs = s.length;
			s += '.';
		}
		while(s.length <= rs + 2) {
			s += '0';
		}
		return s;
	}